package com.cmy.lib_room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.cmy.lib_room.dao.BaseDao
import com.cmy.lib_room.entity.BaseEntity

/**
 *
 */
@Database(entities = [BaseEntity::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {
    abstract val workDao: BaseDao

    companion object {
        private const val DB_NAME = "base.db"
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context):AppDatabase = INSTANCE ?: synchronized(this){
            INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
        }

        private fun buildDatabase(context: Context): AppDatabase = Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            DB_NAME)
            .allowMainThreadQueries()
            .build()

    }

}